#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define lin read<ll>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
	T x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 1e6 + 10;

int n, siz[N], son[N], mxd[N];
ll ans[N];
vec G[N];

ll tdfs(int x, int p, int d) {
	mxd[x] = 1; ll tot = d;
	for(auto y : G[x]) if(y ^ p) {
			tot += tdfs(y, x, d + 1); mxd[x] = max(mxd[x], mxd[y] + 1);
		}
	return tot;
}

ll calc(int x, int p) {
	ll res = tdfs(x, 0, 0) * 2; int t = 0;
	for(auto y : G[x]) if(y ^ p && siz[y] == n / 2) chkmax(t, mxd[y]); 
	if(siz[x] == n - n / 2) chkmax(t, mxd[p]);
	if(n % 2 == 0 && t > 0) return res - t;
	t = 0; for(auto y : G[x]) chkmax(t, mxd[y]);
	return res - t;
}

void dfs(int x, int p) {
	siz[x] = 1; for(auto y : G[x]) if(y ^ p) dfs(y, x), siz[x] += siz[y], chkmax(son[x], siz[y]);
	chkmax(son[x], n - siz[x]); if(son[x] <= n / 2) ans[x] = calc(x, p); else ans[x] = -1;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in; rep(i, 1, n - 1) { int u = in, v = in; G[u].eb(v); G[v].eb(u); }
	dfs(1, 0); rep(i, 1, n) printf("%lld\n", ans[i]);
	return 0;
}
